﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Node<T>
{
    public T Data { get; set; }
    public Node<T> Next { get; set; }
    public Node(T data)
    {
        Data = data;
        Next = null;
    }
}
class GenericLinkedList<T>
{
    private Node<T> head;
    public void add(T data)
    {
        if (head==null)
        {
            head = new Node<T>(data);
        }
        else
        {
            Node<T> temp = new Node<T>(data);
            temp.Next = head.Next;
            head.Next = temp;
        }
    }
    public void ForEach(Action<T> action)
    {
        Node<T> now = head;
        while(now!=null)
        {
            action(now.Data);
            now = now.Next;
        }
    }
}
    class Program
{
    static void Main(string[] args)
    {
        GenericLinkedList<int> list = new GenericLinkedList<int>();
        list.add(5);
        list.add(10);
        list.add(2);
        list.add(8);

        Console.Write("链表元素: ");
        list.ForEach(x => Console.Write(x + " "));
        Console.WriteLine();
        // 求最大值
        int max = int.MinValue;
        list.ForEach(x => max = Math.Max(max, x));
        Console.WriteLine($"最大值: {max}");

        // 求最小值
        int min = int.MaxValue;
        list.ForEach(x => min = Math.Min(min, x));
        Console.WriteLine($"最小值: {min}");

        // 求和
        int sum = 0;
        list.ForEach(x => sum += x);
        Console.WriteLine($"总和: {sum}");
    }
}

